/*
 * @FileName: app_analog.h
 * @Author: GreyQiu qiushaogui@aikosolar.com
 * @Date: 2023-07-18 11:50:59
 * @LastEditors: GreyQiu qiushaogui@aikosolar.com
 * @LastEditTime: 2023-09-25 10:42:42
 * @Description: 
 * 
 * Copyright (c) 2023, All Rights Reserved. 
 */

#ifndef __APP_ANALOG_H__
#define __APP_ANALOG_H__

#include <stdint.h>
#include "analog.h"

#define ADC_RADIO (3.36f / 4095)

/**
 * @description: 各个信号的采样系数，ADC的1个码值刻度代表的信号幅度
 */
#define COFF_K_VIN     (0.01955f)
#define COFF_B_VIN     (0.0f)

//#define COFF_K_IIN     (0.01217004f)
//#define COFF_B_IIN     (-25.45f) // 电流放大偏置
#define COFF_K_IIN     (0.01224715f)
#define COFF_B_IIN     (-25.7246f) // 电流放大偏置

#define COFF_K_VOUT     (0.01955f)
#define COFF_B_VOUT     (0.0f)

#define COFF_K_IOUT     (0.008059f)
#define COFF_B_IOUT     (-7.65f) // 电流放大偏置

/**
 * @description: 模拟信号ID
 */
typedef enum {
    ANALOG_ID_VIN = 0,
    ANALOG_ID_IIN,
    ANALOG_ID_VOUT,
    ANALOG_ID_IOUT,
    ANALOG_ID_NUM
} anlog_id_t;

extern analog_t analog_tbl[ANALOG_ID_NUM];
extern int g_mos_temp;
extern float g_mos_ftemp;
extern int g_mcu_temp;
extern float g_mcu_ftemp;

/**
 * @description: 模拟信号计算
 * @param id    模拟信号ID
 * @return 无
 */
//static inline void app_analog_conv(uint16_t id, )
//{
//    analog_convert(&analog_tbl[id]);
//    analog_filter(&analog_tbl[id]);
//}

/**
 * @description: 模拟信号值获取（未滤波）
 * @param id    模拟信号ID
 * @return 模拟信号值
 */
static inline float asignal_get_real(uint16_t id)
{
    return analog_tbl[id].real;
}

/**
 * @description: 模拟信号值获取（已滤波）
 * @param id    模拟信号ID
 * @return 已滤波后的模拟信号值
 */
static inline float asignal_get_real_flt(uint16_t id)
{
    return analog_tbl[id].real_flt;
}

/**
 * @description: 模拟信号取值地址
 */
#define ANALOG_VALUE_ADDR_VIN (&(analog_tbl[ANALOG_ID_VIN].real_flt))
#define ANALOG_VALUE_ADDR_IIN (&(analog_tbl[ANALOG_ID_IIN].real_flt))
#define ANALOG_VALUE_ADDR_VOUT (&(analog_tbl[ANALOG_ID_VOUT].real_flt))
#define ANALOG_VALUE_ADDR_IOUT (&(analog_tbl[ANALOG_ID_IOUT].real_flt))
#define ANALOG_VALUE_ADDR_TEMP1 (&g_mos_ftemp)
#define ANALOG_VALUE_ADDR_TEMP2 (&g_mcu_ftemp)

void app_analog_init(void);
void app_analog_run(void);
void app_analog_temp_cal(void);
int app_analog_get_temp(void);
float app_analog_get_wh(void);
void app_analog_power(void);
void app_analog_calibrate_curr(void);
#endif // __APP_ANALOG_H__
